#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
using namespace std;
struct T{
	int a,b,c;
}E[200001];
int pre[5001];
bool cmp(struct T a,struct T b){
	return a.c<b.c;
}
int find(int x){
	if(x!=pre[x]) pre[x]=find(pre[x]);
	return pre[x];
}
int N;
int main()
{
    int n,m,a,b,c,ans=0,i;
	cin>>n>>m;
	for(i=1;i<=n;i++)
	pre[i]=i;
	for(i=0;i<m;i++)
	cin>>E[i].a>>E[i].b>>E[i].c;
	sort(E,E+m,cmp);
	for(i=0;i<m;i++){
		if(find(E[i].a)!=find(E[i].b)){
			pre[find(E[i].a)]=find(E[i].b);
			N+=E[i].c;
			ans++;
			if(ans==n-1) break;
		}
	}	
	if(ans==n-1)
	cout<<N;
	else
	cout<<"orz";
}